package day0920;

import java.util.*;

class Cat implements  Comparable{
    int age;

    @Override
    public String toString() {
        return "Cat{" +
                "age=" + age +
                '}';
    }

    public int compareTo(Object o) {//把Cat对象存入TreeMap里边自动调用CompareTom进行排序
        return  this.age - ((Cat)o).age;//升序
    }
}





public class T1 {
    public static void main(String[] args) {
        Map<String, Cat>  map=new TreeMap();

        Cat  tom=new Cat();          tom.age=3;
        Cat  blackPolice=new Cat();  blackPolice.age=1;

        map.put("汤姆猫",tom);//1.
        map.put("黑猫警长",blackPolice);

        Collection<Cat> s=map.values();//3.value获取所有的value
        Iterator<Cat>  ite=s.iterator();//A.
        while(ite.hasNext()){//B.
            Cat c=ite.next();//C
            System.out.println("c = " + c);
        }

        //2.获取元素：获取map中所有的键值对entry  entrySet()
        Set<Map.Entry<String,Cat>> house=map.entrySet();
        for(Map.Entry<String,Cat>   e :house){
            System.out.println("e.getKey() = " + e.getKey());
            System.out.println("e.getKey() = " + e.getValue());
        }
    }




}
//2.获取元素：
// 只获取map中所有key keySet()
// 只获取map中所有value vlaues()
// 获取map中所有的键值对entry  entrySet()


//entrySet()获取了两个元素类型是entry
//                              <key的类型String，value的类型是cat对象>
